home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / simula / books / books.lha / kirkerud / studset.sim < prev    next >
Text File  |  1993-08-16  |  13KB  |  333 lines

  1. % ****************************************************************
  2. % *                                                              *
  3. % *  This is the program constructed in example 14.1 of          *
  4. % *  Object Oriented Programming with Simula by Bj|rn Kirkerud;  *
  5. % *                                                              *
  6. % ****************************************************************
  7.  
  8. external class Settools;
  9.  
  10. Settools begin
  11.  
  12.  
  13. % ****************************************************************
  14. % *                                                              *
  15. % *  Declarations of auxiliary procedures:                       *
  16. % *                                                              *
  17. % ****************************************************************
  18.  
  19.   character procedure prompt_for_char(prompt);  text prompt;
  20.     begin
  21.       outtext(prompt); breakoutimage; inimage;
  22.       prompt_for_char := inchar;
  23.     end of prompt_for_char;
  24.  
  25.   integer procedure prompt_for_int(prompt);  text prompt;
  26.     begin
  27.       outtext(prompt); breakoutimage; inimage;
  28.       prompt_for_int := inint;
  29.     end of prompt_for_int;
  30.  
  31.   real procedure prompt_for_real(prompt);  text prompt;
  32.     begin
  33.       outtext(prompt); breakoutimage; inimage;
  34.       prompt_for_real := inreal;
  35.     end of prompt_for_real;
  36.  
  37.   text procedure prompt_for_text(prompt);  text prompt;
  38.     begin
  39.       outtext(prompt); breakoutimage; inimage;
  40.       prompt_for_text :- intext(80).strip;
  41.     end of prompt_for_text;
  42.  
  43.   Boolean procedure prompt_for_bool(prompt);  text prompt;
  44.     begin character c;
  45.       outtext(prompt); breakoutimage; inimage;
  46.       c := inchar;
  47.       prompt_for_bool := c = 'y' or c = 'Y';
  48.     end of prompt_for_bool;
  49.  
  50.   procedure User_message(message); text message;
  51.     begin outtext(message); outimage end;
  52.  
  53.  
  54.  
  55. % ****************************************************************
  56. % *                                                              *
  57. % *  The class Student:                                          *
  58. % *                                                              *
  59. % ****************************************************************
  60.  
  61.   element class Student(ident); text ident;
  62.     begin
  63.  
  64.       integer year, month, day, form;
  65.       Boolean female;
  66.       character math_grade, eng_grade, hist_grade;
  67.  
  68.       text procedure key; key :- ident;
  69.  
  70.     ! A variable to hold a reference to the next Student in a pointer chain:   ;
  71.       ref(Student) next_in_chain;
  72.  
  73.       procedure read;
  74.         begin
  75.           year            := prompt_for_int("Year of birth? ");
  76.           month        := prompt_for_int("Month? ");
  77.           day            := prompt_for_int("Day? ");
  78.           form            := prompt_for_int("Form? ");
  79.           female        := prompt_for_bool("Female? ");
  80.           math_grade        := prompt_for_char("Grade in mathematics? ");
  81.           eng_grade        := prompt_for_char("Grade in English? ");
  82.           hist_grade        := prompt_for_char("Grade in history? ");
  83.         end of Student'read;
  84.  
  85.       procedure display;
  86.         begin
  87.           outtext("Data for student: "); outtext(ident);
  88.           outtext(".  Born: ");    outint(day,     2);   outchar('/');
  89.              outint(month,   2);   outchar('/');
  90.              outint(year,    4);
  91.           outtext(if female then ".  Female."  else ".  Male."); outimage;
  92.           outtext("  Form: "); outint(form, 1);
  93.           outtext(".  Current grades:");
  94.             outtext("  Mathematics: ");  outchar(math_grade);
  95.             outtext("  English: ");      outchar(eng_grade);
  96.             outtext("  History: ");      outchar(hist_grade); outimage;
  97.         end of Student'display;
  98.  
  99.       procedure change;
  100.         begin character attribute;
  101.           attribute := prompt_for_char("What do you want  to change? ");
  102.           if attribute = 'y' then year       
  103.              := prompt_for_int("New birth year? ")      else
  104.           if attribute = 'm' then month      
  105.              := prompt_for_int("New birth month? ")       else
  106.           if attribute = 'd' then day        
  107.              := prompt_for_int("New day of birth? ")      else
  108.           if attribute = 'f' then form       
  109.              := prompt_for_int("New form number? ")       else
  110.           if attribute = 's' then female     
  111.              := prompt_for_bool("Female? ")               else
  112.           if attribute = 'a' then math_grade 
  113.              := prompt_for_char("New grade in math? ")    else
  114.           if attribute = 'e' then eng_grade  
  115.              := prompt_for_char("New grade in English? ") else
  116.           if attribute = 'h' then hist_grade 
  117.              := prompt_for_char("New grade in history? ")
  118.         else begin
  119.               User_message("You can change one of the  following attributes:");
  120.               User_message("  m: Birth month");    
  121.               User_message("  d: Day of birth");
  122.               User_message("  f: Form number");    
  123.               User_message("  s: Sex");
  124.               User_message("  a: Grade in mathematics");
  125.               User_message("  e: Grade in English");
  126.               User_message("  h: Grade in history");
  127.               change; ! Observe that this is an invocation of the procedure 
  128.                       ! being declared. The effect is that user is given
  129.                       ! another chance to change;
  130.             end;
  131.         end of Student'change;
  132.  
  133.       character procedure worst_grade;
  134.         worst_grade := max(math_grade, max(eng_grade,  hist_grade));
  135.  
  136.       procedure put_in_record(outf); ref(outfile) outf;
  137.         inspect outf do 
  138.           begin
  139.             outtext(ident); setpos(11);
  140.             outint(year, 5); outint(month, 3); outint(day, 3); 
  141.             outint(form, 2); outint(if female then 1 else 0, 2);
  142.             outchar(math_grade); outchar(eng_grade); outchar(hist_grade); 
  143.           end;
  144.  
  145.       procedure get_from_record(inf); ref(infile) inf;
  146.         inspect inf do 
  147.           begin
  148.             year := inint; month := inint; day := inint; 
  149.             form := inint; female := inint = 1;
  150.             math_grade := inchar; eng_grade := inchar; hist_grade := inchar; 
  151.           end;
  152.  
  153.     end of Student;
  154.  
  155.  
  156. % ****************************************************************
  157. % *                                                              *
  158. % *  Declaration of a variable to keep a reference to            *
  159. % *  an ordered set of Students:                                 *
  160. % *                                                              *
  161. % ****************************************************************
  162.  
  163.   ref(ordered_set) The_students;
  164.  
  165.  
  166. % ****************************************************************
  167. % *                                                              *
  168. % *  Declarations of command procedures:                         *
  169. % *                                                              *
  170. % ****************************************************************
  171.  
  172.   procedure Give_help;
  173.     begin
  174.       User_message("The legal commands are: "); 
  175.       User_message("   ?:  Help (writes this text)"); 
  176.       User_message("   N:  To enter data about a new student"); 
  177.       User_message("   W:  Writes data about a specified student"); 
  178.       User_message("   L:  Writes a list with all students"); 
  179.       User_message("   C:  Changes data about a specified student"); 
  180.       User_message("   R:  Removes all data about a specified student"); 
  181.       User_message("   P:  Puts all data to file ""stud.dta"""); 
  182.       User_message("   G:  Gets data from file ""stud.dta"""); 
  183.       User_message("   B:  Writes students with bad grades"); 
  184.       User_message("   Q:  Quit (the program execution stops)"); 
  185.     end of Give_help;
  186.  
  187.   procedure Enter_student;
  188.     begin  ref(Student) a_student; Boolean ident_exists;
  189.       a_student :- new Student(prompt_for_text("Identity number? "));
  190.       The_students.add_element(a_student, ident_exists);
  191.       if ident_exists
  192.         then User_message("The identity number " & a_student.key &
  193.                           " is already  in use!")
  194.         else begin
  195.             a_student.read;
  196.             User_message("The data for student " & a_student.key &
  197.                          " have been stored.");
  198.           end;
  199.     end of Enter_student;
  200.  
  201.   procedure Write_student;
  202.     begin ref(Student) a_student;
  203.       a_student :- The_students.find_element(prompt_for_text("Id. number? "));
  204.       if a_student == none
  205.         then User_message("No student with that  identity number!")
  206.         else a_student.display;
  207.     end of Write_student;
  208.  
  209.   procedure List_students;
  210.     begin ref(Student) a_student;
  211.       User_message("The students for which data have  been entered:");
  212.       a_student :- The_students.first_element;
  213.       while a_student =/= none do
  214.         begin
  215.           a_student.display;
  216.           a_student :- The_students.next_element;
  217.         end;
  218.     end of List_students;
  219.  
  220.   procedure Change_student;
  221.     begin ref(Student) a_student; 
  222.       a_student :- The_students.find_element(prompt_for_text("Id. number? "));
  223.       if a_student == none
  224.         then User_message("No student with that identity number!")
  225.         else begin a_student.display;  a_student.change end;
  226.     end of Change_student;
  227.  
  228.   procedure Remove_student;
  229.     begin integer ident_number;  Boolean no_such_student;
  230.       ident_number := prompt_for_int("Identity number? ");
  231.       The_students.Remove_element(prompt_for_text("Id. number? "),  
  232.                                   no_such_student);
  233.       if no_such_student
  234.         then User_message("No student with that identity  number!")
  235.         else User_message("The student has been removed!");
  236.     end of Remove_student;
  237.  
  238.   procedure Put_to_file;
  239.     begin ref(Student) a_student;
  240.       inspect new outfile("stud.dta") do
  241.         begin
  242.           open(blanks(28));
  243.           a_student :- The_students.first_element;
  244.           while a_student =/= none do
  245.             begin
  246.               a_student.put_in_record(this outfile);
  247.               outimage;
  248.               a_student :- The_students.next_element;
  249.             end;
  250.           close;
  251.         end;
  252.     end of Put_to_file;
  253.  
  254.   procedure Get_from_file;
  255.     begin ref(Student) a_student; Boolean ident_exists;
  256.       inspect new infile("stud.dta") do
  257.         begin
  258.           open(blanks(28)); inimage;
  259.           while not endfile do
  260.             begin
  261.               a_student :- new Student(intext(10).strip);
  262.               a_student.get_from_record(this infile);
  263.               The_students.add_element(a_student, ident_exists);
  264.               inimage;
  265.             end;
  266.           close;
  267.         end;
  268.     end of Get_from_file;
  269.  
  270.   procedure Bad_grades;
  271.     begin character grade_limit;  ref(Student) a_student;
  272.       grade_limit := prompt_for_char("Grade limit? ");
  273.       a_student :- The_students.first_element;
  274.       while a_student =/= none do
  275.         begin
  276.           if a_student.worst_grade ge grade_limit then a_student.display;
  277.           a_student :- The_students.next_element;
  278.         end;
  279.     end of Bad_grades;
  280.  
  281.  
  282.   procedure Unknown_command(c); character c;
  283.     begin
  284.       outtext("   You gave the command '"); outchar(c);
  285.       outtext("'.  This is not among the legal commands.");  outimage;
  286.       outtext("   Type ? if you don't remember the legal commands"); 
  287.       outimage;
  288.     end of Unknown command;
  289.  
  290.  
  291. % ****************************************************************
  292. % *                                                              *
  293. % *  Declaration of a variable to keep the latest command        *
  294. % *  typed by the user:                                          *
  295. % *                                                              *
  296. % ****************************************************************
  297.  
  298.   character command;
  299.  
  300.  
  301. % ****************************************************************
  302. % *                                                              *
  303. % *  That was the last declaration.                              *
  304. % *  Now come the imperatives of the program:                    *
  305. % *                                                              *
  306. % ****************************************************************
  307.  
  308. % *  First, initialization of the data-structure:  ;
  309.  
  310.   The_students :- new ordered_set;
  311.  
  312. % *  Then, read and execute commands typed by the user:  ;
  313.  
  314.   command := prompt_for_char("Type your first command  (? for help) > ");
  315.   while command ne 'Q' do
  316.     begin
  317.       if command = '?' then Give_help         else
  318.       if command = 'N' then Enter_student  else
  319.       if command = 'W' then Write_student  else
  320.       if command = 'L' then List_students  else
  321.       if command = 'C' then Change_student else
  322.       if command = 'R' then Remove_student else
  323.       if command = 'P' then Put_to_file    else
  324.       if command = 'G' then Get_from_file  else
  325.       if command = 'B' then Bad_grades
  326.       else Unknown_command(command);
  327.       command := prompt_for_char("Your next command > ");
  328.     end;
  329.  
  330.   User_message("Bye");
  331.  
  332. end
  333.